var redis = require('redis');
var client = redis.createClient();

// 创建一个名为LeaderBoard的函数，它接收一个键名作为参数。
function LeaderBoard(key) {
    this.key = key;
}

// 执行命令ZADD将一个成员插入到一个排序集中。
LeaderBoard.prototype.addUser = function (username, score) {
    client.zadd([this.key, score, username], function (err, replies) {
        console.log('user', username, 'added to the leaderboard!');
    })
}

// 执行命令ZREM从排序集中删除成员。
LeaderBoard.prototype.removeUser = function (username) {
    client.zrem(this.key, username, function (err, replies) {
        console.log("user", username, "redmoved successfully!");
    })
}

// 方法getUserScoreAndRank通过执行命令ZSCORE和ZREVRANK来显示用户的排名和分数
LeaderBoard.prototype.getUserScoreAndRank = function(username) { 
  var leaderboardKey = this.key; 
  client.zscore(leaderboardKey, username, function(err, zscoreReply) { 
    client.zrevrank(leaderboardKey, username, function( err, zrevrankReply) { 
      console.log("\nDetails of " + username + ":"); 
      console.log("Score:", zscoreReply + ", Rank: #" + (zrevrankReply + 1)); 
    });
  }); 
};

// ZREVRANGE 返回从最高到最低成绩排列的成员范围
LeaderBoard.prototype.showTopUsers = function (quantity) {
    client.zrevrange([this.key, 0, quantity - 1, "WITHSCORES"], function (err, reply) {
        console.log("\nTop", quantity, "users:");
        for (var i = 0, rank = 1; i < reply.length; i += 2, rank++) {
            console.log("#" + rank, "User: " + reply[i] + ", score:", reply[i + 1]);
        }
    })
}

// 方法getUsersAroundUser用于获取在排行榜中相对于给定用户排名的用户列表
LeaderBoard.prototype.getUsersAroundUser = function (username, quantity, callback) {
    var leaderboardKey = this.key;
    client.zrevrank(leaderboardKey, username, function (err, zrevrankReply) {
        var startOffset = Math.floor(zrevrankReply - (quantity / 2) + 1); //在这个排序中，在中间的部位
        if (startOffset < 0) {
            startOffset = 0;
        }
        var endOffset = startOffset + quantity - 1;
        client.zrevrange([leaderboardKey, startOffset, endOffset, "WITHSCORES"], function (err, zrevrangeReply) {
            var users = [];
            for (var i = 0, rank = 1; i < zrevrangeReply.length; i += 2, rank++) {
                var user = {
                    rank: startOffset + rank,
                    score: zrevrangeReply[i + 1],
                    username: zrevrangeReply[i],
                };
                users.push(user);
            }
            callback(users);
        });
    });
};


var leaderBoard = new LeaderBoard("game-score");
leaderBoard.addUser("Arthur", 70);
leaderBoard.addUser("KC", 20);
leaderBoard.addUser("Maxwell", 10);
leaderBoard.addUser("Patrik", 30);
leaderBoard.addUser("Ana", 60);
leaderBoard.addUser("Felipe", 40);
leaderBoard.addUser("Renata", 50);
leaderBoard.addUser("Hugo", 80);
leaderBoard.removeUser("Arthur");
leaderBoard.getUserScoreAndRank("Maxwell"); //显示名称为maxwell用户的排名
leaderBoard.showTopUsers(3); //显示前3名

leaderBoard.getUsersAroundUser("Felipe", 5, function (users) { 
    console.log("\nUsers around Felipe:");
    users.forEach(function (user) {
        console.log("#" + user.rank, "User:", user.username + ", score:", user.score);
    });
    client.quit(); 
});
